<html>
<head><meta charset="utf-8"><title>const_evaluable_checked and encapsulation · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const_evaluable_checked.20and.20encapsulation.html">const_evaluable_checked and encapsulation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225726840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const_evaluable_checked%20and%20encapsulation/near/225726840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const_evaluable_checked.20and.20encapsulation.html#225726840">(Feb 09 2021 at 17:35)</a>:</h4>
<p>Playing around with const_evaluable_checked. Is there a way to avoid exposing various details about your types internal representation in the public API when doing things with it? E.g. Say I have:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">BitArray</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">BITS</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">([</span><span class="kt">usize</span><span class="p">;</span><span class="w"> </span><span class="n">bits_to_len</span><span class="p">(</span><span class="n">BITS</span><span class="p">)])</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="p">[</span><span class="kt">usize</span><span class="p">;</span><span class="w"> </span><span class="n">bits_to_len</span><span class="p">(</span><span class="n">BITS</span><span class="p">)]</span>: <span class="nb">Sized</span><span class="p">;</span><span class="w"></span>

<span class="c1">// Not public API, don't rely on the result, needs to be</span>
<span class="c1">// public to avoid private-in-public issues...</span>
<span class="cp">#[doc(hidden)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">bits_to_len</span><span class="p">(</span><span class="n">n</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">w</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">core</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">w</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">w</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="c1">// ...</span>
</code></pre></div>
<p>Then say I'm interested in the future in changing this to use a multi-level array, that is — it would have an additional, smaller internal bitarray with 1 bit per index in the big array, where a set bit in the small array indicates that the big array is non-empty in the <code>usize</code> at that index — I can show what this would look like but... well... it has multiple arrays, with more bounds.</p>
<p>It's a bit contrived (1 and 2 level should likely be separate types), but ISTM like that change would probably be breaking, no matter how you cut it. E.g. you lose a lot of encapsulation ability when using const_evaluable_checked. Ideally, the details of what I use inside would be opaque and free to change...</p>
<p>Maybe i'm missing something though.</p>



<a name="225736276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const_evaluable_checked%20and%20encapsulation/near/225736276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const_evaluable_checked.20and.20encapsulation.html#225736276">(Feb 09 2021 at 18:38)</a>:</h4>
<p>the design of <code>const_evaluatable_checked</code> is not yet fully fleshed out and my current plan is to start with always requiring these bounds to find pain points and think about how to solve them</p>



<a name="225736458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const_evaluable_checked%20and%20encapsulation/near/225736458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const_evaluable_checked.20and.20encapsulation.html#225736458">(Feb 09 2021 at 18:39)</a>:</h4>
<p>my personal goal here would be to keep requiring these bounds but add an option for an auther to explicitly opt out of some of the bounds if they are know to always evaluate successfully</p>



<a name="225736661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const_evaluable_checked%20and%20encapsulation/near/225736661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const_evaluable_checked.20and.20encapsulation.html#225736661">(Feb 09 2021 at 18:40)</a>:</h4>
<p>in your example the evaluation would fail for <code>BITS = usize::MAX</code>, I am however not sure if that's something you should be concerned about, considering that you hit the size limit long before that</p>



<a name="225748754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const_evaluable_checked%20and%20encapsulation/near/225748754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const_evaluable_checked.20and.20encapsulation.html#225748754">(Feb 09 2021 at 20:03)</a>:</h4>
<blockquote>
<p>in your example the evaluation would fail for BITS = usize::MAX</p>
</blockquote>
<p>right, I tried with <code>(n / w) + ((n % w == 0) as usize</code> which can't overflow too. it didn't help and i think its meaningfully less clear so i went back.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>